package 力扣日常刷题.木22一月.第08天0116;

/**
 * @author 帅小伙
 * @date 2022/1/16
 * @description
 */
public class Demo08动态规划中等最小路径和 {


    public static void main(String[] args) {
        Demo08动态规划中等最小路径和 test = new Demo08动态规划中等最小路径和();
        test.minPathSum(new int[][]{{1, 3}, {1, 5}});
    }


    public int minPathSum(int[][] grid) {
        int n = grid.length;
        int m = grid[0].length;
        int[] dp = new int[m];

        for (int i = 0; i < m; i++) {
            if (i == 0) {
                dp[i] = grid[0][i];
            } else {
                dp[i] = dp[i - 1] + grid[0][i];
            }
        }

        for (int i = 1; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (j == 0) {
                    dp[j] = dp[j] + grid[i][j];
                } else {
                    dp[j] = Math.min(dp[j], dp[j - 1]) + grid[i][j];
                }
            }
        }
        return dp[m - 1];
    }
}
